package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public int insertGoods(Goods goods) {
        return 0;
    }

    @Override
    public int updateGoods(Goods goods) {
        return 0;
    }

    @Override
    public int deleteGoods(Integer goods) {
        return 0;
    }


    @Override
    public List<Goods> selectGoods(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select * from goods where 1=1 ");
        List param = new ArrayList();
        //判断goodsName是否有数据，如果有，则拼接
        String goodsName = map.get("goodsName");
        if(goodsName!=null && !"".equals(goodsName)){
            stb.append(" and goodsName like ? ");
            param.add("%"+goodsName+"%");
        }
        //判断shopName是否有数据，如果有，则拼接
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            stb.append(" and goodsShopName like ? ");
            param.add("%"+shopName+"%");
        }
        //上下架状态
        String goodsStatus = map.get("goodsStatus");
        System.out.println(goodsStatus);
        if(goodsStatus!=null && !"".equals(goodsStatus)){
            stb.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        stb.append(" limit ?,? ");
        param.add((pageNum - 1) * pageSize);
        param.add(pageSize);
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                Goods goods = new Goods(
                        resultSet.getInt(1),
                        resultSet.getDate(2),
                        resultSet.getInt(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getDouble(6),
                        resultSet.getInt(7),
                        resultSet.getString(8),
                        resultSet.getInt(9),
                        resultSet.getString(10),
                        resultSet.getInt(11),
                        resultSet.getInt(12)
                );
                list.add(goods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectGoodsCount(Map<String, String> map) {
        int count = 0;
        //动态sql和动态数组
        StringBuffer stb = new StringBuffer("select count(1) from goods where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String goodsName = map.get("goodsName");
        if(goodsName!=null && !"".equals(goodsName)){
            stb.append(" and goodsName like ? ");
            param.add("%"+goodsName+"%");
        }
        //判断shopName是否有数据，如果有，则拼接
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            stb.append(" and goodsShopName like ? ");
            param.add("%"+shopName+"%");
        }
        //
        String goodsStatus = map.get("goodsStatus");
        if(goodsStatus!=null && !"".equals(goodsStatus)){
            stb.append(" and goodsStatus = ? ");
            param.add(goodsStatus);
        }
        //判断日期是否存在s
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            stb.append(" and addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = stb.toString();
        Object object[] = param.toArray();
        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }

    @Override
    public int updateStatus(Goods goods) {
        String sql = "update goods set goodsStatus=? where goodsId=?";
        Object obj[] = {
           goods.getGoodsStatus(),
           goods.getGoodsId()
        };
        int row = JDBCDruidUtil.update(sql, obj);
        return row;
    }
}
